//画时序图
var finalActTime = 0;// 画时序图需要的最后动作时间(最大值)
var drawData=[];//画时序图需要的数据
var subStation = [];// 站名
function drawActSvg(){
	var drawSvgObj=this;
	var svg = new drawSvgFigure("#svg");
	svg.drag();
	var svgWidth=Number($("#svg").parent().width())-150;//svg宽度
	var svgGroup = svg.setGroup();// 创建一个svg元素组，将所有的元素放在里面，方便整体移动位置
	var time1 = 0,time2 = 50,time3 = 100,time4 = 950,time5 = 1000;//五个时间段的边界值
	//w1:0-50  w2:50-100  w3:100-950  w4:950-1000  w5:1000+   
	var w1 = 0, w2 = 0, w3 = 0, w4 = 0,w5=0,timeLineNum=0;//时间轴段数;
	var ratio1=2,ratio2 = 10,ratio3 = 100,ratio4 = 2,ratio5 = 200;//最小刻度
	var sy = 110;// 时间轴起始点纵坐标,初始值在110
	var color = "#696969";//刻度字体颜色
	var txtPos = [];// 存放所有红色字体的横坐标,以及时间轴刻度的坐标
	var fontSize;//字体大小
	var scaleTextOffsetY;//刻度字体距离时间轴的距离
	if(isEnlarge){
		fontSize=21;
		scaleTextOffsetY=20;
	}else{
		fontSize=14;
		scaleTextOffsetY=15;
	}
	$("#span").css("font-size",fontSize+"px");
	//重绘svg
	this.refreshSvg=function(){
		svg = new drawSvgFigure("#svg");
		svg.drag();
		drawSvgObj.reInitSvg();
		$("#svg").width("2000").height("500");
		svgWidth = Number($("#svg").parent().width())-150;
	}
	this.reInitSvg=function(){
		svgGroup.remove();
		svgGroup = svg.setGroup();
	}
	this.refreshData=function(){
		w1 = 0, w2 = 0, w3 = 0, w4 = 0,w5=0,timeLineNum=0;//时间轴段数;
		ratio2 = 10;// w2最小刻度
		ratio4 = 2;// w4最小刻度
		ratio5 = 200;// w5最小刻度
	}
	this.reDrawSvg=function(){
		drawSvgObj.reInitSvg();
		subStation = [];
		actTimeInfo = [];
		allActInfo = [];
		errorList = [];
	}
	//画刻度
	function drawScale(ratio, timeRatio, time1, time2, sx) {
		for (var i = 0; i <= (time2 - time1) / ratio; i++) {
			var x = ratio * i * timeRatio + sx;
			var time = time1 + ratio * i;
			var timeStr = time.toString();
			if (timeStr == "0" ||timeStr == "10" || timeStr == "50" || timeStr == "100"  || timeStr == "500"|| timeStr == "950"
					|| timeStr == "1000" || timeStr == "1500" || timeStr == "2000") {
				svgGroup.add(svg.drawLine(x, sy, x, sy - 15).attr({
					"stroke" : color,
					"stroke-width" : 1
				}));
				$("#span").html(timeStr);
				var tLen = $("#span").width();
				svgGroup.add(svg.drawText(x - tLen/2, sy + scaleTextOffsetY, timeStr).attr({
					fill : color,
					"font-size":fontSize
				}));
				
				txtPos.push([ x - tLen/2, x + tLen/2]);
			} 
			/*else {
				svgGroup.add(svg.drawLine(x, sy, x, sy - 10).attr({
					"stroke" : color
				}));
			}*/
		}
	}
	// 以50,100,950,1000为分界时间点分五段画时间轴,10-50和950-1000的刻度是一样的
	this.initTimeLineData=function(timeLineScale) {
		svgWidth = svgWidth*timeLineScale;
		w2 = svgWidth * 0.5 / 10;
		w3 = svgWidth * 1 / 10;
		if (finalActTime > time5) {
			//最大值大于1000，最大值如果在1050以内，就取最大值为1050
			finalActTime=finalActTime>1050?finalActTime:1050;
			w1 = svgWidth * 3/ 10;
			//需要判断数据集中在w4还是w5
			if(svgData.getDataRange()){
				var lt1000=svgData.getLt1000();
				//time5=Number(lt1000.toFixed(0));
				w4 = svgWidth * 3.5/ 10;
				w5 = svgWidth * 2 / 10;
			}else{
				var gt1000=svgData.getGt1000();
				if(finalActTime!=gt1000){
					//time5=Number(gt1000.toFixed(0));
					ratio5 = 10;// 最小刻度
				}
				w4 = svgWidth * 2 / 10;
				w5 = svgWidth * 3.5/ 10;
			}
			timeLineNum = 5;
		}else if (finalActTime > time4) {
			//time5=Number(finalActTime.toFixed(0));
			w1 = svgWidth * 5 /10;
			w4 = svgWidth * 3.5/ 10;
			timeLineNum = 4;
		} else if (finalActTime > time3) {
		//	time4 = Number(finalActTime.toFixed(0));
			w1 = svgWidth * 5.5 / 10;
			w3 = svgWidth * 4 / 10
			ratio2 = 5;// 最小刻度
			timeLineNum = 3;
		} else if (finalActTime > time2) {
			w1 = svgWidth * 1 / 2;
			w2 = svgWidth * 1 / 2;
			ratio2 = 2;//最小刻度
			timeLineNum = 2;
		} else {
			//只有0-50
			w1 = svgWidth;
			timeLineNum = 1;
			//time1=Number(Math.floor(svgData.getMinTime(drawData)/10))*10;
			if(finalActTime==0){
				//最终的时间为0，说明最大值小于0.5
				time2=1;
			}
		}
		drawTimeLine();
	}
	function drawTimeLine(){
		txtPos = [];
		var title = svg.drawText(0, sy, "时间轴/ms").attr({
			fill : "black",
		});
		svgGroup.add(title);
		// 第一段时间节点在50以内的
		var sx1 = 80;// 时间轴起始点横坐标
		var ex1 = sx1 + w1;// 时间轴结束点横坐标
		var endx=ex1;
		var ey = sy;// 时间轴结束点纵坐标
		var timeLine1 = svg.drawLine(sx1, sy, ex1, ey).attr({
			"stroke" : color,
			"stroke-width" : 2
		});
		svgGroup.add(timeLine1);
		var timeRatio1 = (ex1 - sx1) / (time2 - time1);
		// 画刻度线
		var ratio1 = 2;// 最小刻度
		drawScale(ratio1, timeRatio1, time1, time2, sx1);
		if (timeLineNum >= 2) {
			// 第二段,50-100
			var sx2 = ex1;// 时间轴起始点横坐标
			var ex2 = sx2 + w2;// 时间轴结束点横坐标
			endx=ex2;
			var timeLine2 = svg.drawLine(sx2, sy, ex2, ey).attr({
				"stroke" : color,
				"stroke-width" : 2
			});
			svgGroup.add(timeLine2);
			var timeRatio2 = (ex2 - sx2) / (time3 - time2);
			drawScale(ratio2, timeRatio2, time2, time3, sx2);
		}
		if (timeLineNum >= 3) {
			// 第三段100-950
			var sx3 = ex2;
			var ex3 = sx3 + w3;
			endx=ex3;
			var timeLine3 = svg.drawLine(sx3, sy, ex3, ey).attr({
				"stroke" : color,
				"stroke-width" : 2
			});
			svgGroup.add(timeLine3);
			var timeRatio3 = (ex3 - sx3) / (time4 - time3);
			var ratio3 = 100;// 最小刻度
			drawScale(ratio3, timeRatio3, time3, time4, sx3);
		}
		if (timeLineNum >= 4) {
			// 第四段950-1000
			var sx4 = ex3;
			var ex4 = sx4 + w4;
			endx=ex4;
			var timeLine4 = svg.drawLine(sx4, sy, ex4, ey).attr({
				"stroke" : color,
				"stroke-width" : 2
			});
			svgGroup.add(timeLine4);
			var timeRatio4 = (ex4 - sx4) / (time5 - time4);
			// 第四段950+，画刻度
			drawScale(ratio4, timeRatio4, time4, time5, sx4);
		}
		if (timeLineNum == 5) {
			// 第五段1000+
			var sx5 = ex4;
			var ex5 = sx5 + w5;
			endx=ex5;
			var timeLine5 = svg.drawLine(sx5, sy, ex5, ey).attr({
				"stroke" : color,
				"stroke-width" : 2
			});
			svgGroup.add(timeLine5);
			var timeRatio5= (ex5 - sx5) / (finalActTime - time5);
			// 第五段1000+，画刻度
			drawScale(ratio5, timeRatio5, time5, finalActTime, sx5);
		}
		//时间轴的结束位置画箭头
		var endArrow = svg.drawLine(endx, sy, endx+40, sy).attr({
			"stroke":"black",
			"marker-end":svg.drawArrow().attr({
				"fill":color
			})
		});
		svgGroup.add(endArrow);
		// 画时序图
		drawActTime(sx1, sx2, sx3, sx4,sx5, timeRatio1, timeRatio2, timeRatio3, timeRatio4,timeRatio5);
	}
	// 画时序图，根据总长和总时间按比例，在时间轴上取点
	var offsetY;//字体距离矩形框顶部距离
	function drawActTime(sx1, sx2, sx3, sx4,sx5, timeRatio1, timeRatio2, timeRatio3, timeRatio4, timeRatio5) {
		var scaleTxtColor=color;
		if(isEnlarge){
			var rectH = 40;// 矩形高
			var addArrowLine=50;//增加的剪头线长度
			offsetY=30;
		}else{
			var rectH = 30;// 矩形高
			var addArrowLine=40;//增加的剪头线长度
			offsetY=20;
		}
		var rectW = 70;// 矩形宽
		var minRectPos = 0;// 矩形框最小纵坐标
		var maxRectPos = 170;// 矩形框最大纵坐标
		var maxRectPosX = 2000;// 矩形框最大横坐标
		
		// 先画上面再画下面，后画该站的动作信息
		for (var i = 0; i < drawData.length; i++) {
			if (!$.isEmptyObject(drawData[0])) {
				var station1 = svg.drawText(0, sy - 90, subStation[0]).attr({
					fill : "black",
					"font-size":fontSize
				});
				svgGroup.add(station1);
			}
			if (!$.isEmptyObject(drawData[1])) {
				// 站2
				var station2 = svg.drawText(0, sy + 100, subStation[1]).attr({
					fill : "black",
					"font-size":fontSize
				});
				svgGroup.add(station2);
			}
			var rectLeftArr = [];// 存放同侧所有矩形框的left值和距离时间轴的高度
			var rectLeft = 0;
			var dotLeft = 0;
			var faultInfo = drawData[i];
			var ArrowLineLength = 30;
			for ( var time in faultInfo) {
				var t =Number(time) ;
				if (t <= time2) {
					var endX = (t - time1) * timeRatio1 + sx1;
				} else if (t <= time3) {
					var endX = (t - time2) * timeRatio2 + sx2;
				} else if (t <= time4) {
					var endX = (t - time3) * timeRatio3 + sx3;
				} else if (t <= time5) {
					var endX = (t - time4) * timeRatio4 + sx4;
				} else {
					var endX = (t - time5) * timeRatio5 + sx5;
				}
				var startX = endX;
				$("#span").html(t.toString());// 判断时间宽度
				var fontLen = $("#span").width();
				if (i == 0) {
					// 站1画上面
					var endY = sy - 22;
				} else {
					// 站2画下面
					var endY = sy + scaleTextOffsetY+10;
				}
				var redLine = svg.drawLine(endX, sy, endX, sy - 10).attr({
					"stroke" :scaleTxtColor,
					"stroke-width" : 1
				});
				var tx = endX - fontLen/2;
				var ty = sy+scaleTextOffsetY;
				dotLeft = tx + fontLen;
				for ( var j in faultInfo[time]) {
					var g = svg.setGroup();
					// 比较判断红色坐标值是否要显示，如果没有重叠就显示，否则就不显示。
					for (var k = 0; k < txtPos.length; k++) {
						if ((tx <= txtPos[k][0] && txtPos[k][0] <= dotLeft)
								|| (tx <= txtPos[k][1] && txtPos[k][1] <= dotLeft)
								|| (txtPos[k][0] <= tx && txtPos[k][1] >= dotLeft)) {
							break;
						} else if (k == txtPos.length - 1) {
							var redTxt = svg.drawText(tx, ty, t).attr({
								"fill" : scaleTxtColor,
								"font-size":fontSize
							});
							g.add(redTxt);
							txtPos.push([ tx, dotLeft ]);
						}
					}
					var str = faultInfo[time][j][0];
					var descstr = faultInfo[time][j][1];
					// 根据文本重新计算矩形框的宽度，保证其不换行
					$("#span").html(str);
					var strWidth = $("#span").width();
					rectW = strWidth + 20;
					var rectStartX = startX - rectW / 2;
					if (rectStartX < 0) {
						rectStartX -= rectStartX;
					}
					ArrowLineLength = 30;
					// 起初箭头线的长度是30，依次比较判断是否会有矩形框重叠，如果有重叠，就增大长度。
					for (var k = 0; k < rectLeftArr.length; k++) {
						if (ArrowLineLength == rectLeftArr[k][1]) {
							if (endX - rectW / 2 < rectLeftArr[k][0]) {
								ArrowLineLength += addArrowLine;
								k = 0;
							}
						}
					}
					if (i == 0) {
						// 站1画上面
						var startY = endY - ArrowLineLength;
						var rectStartY = endY - ArrowLineLength - rectH;
					} else {
						// 站2画下面
						var startY = endY + ArrowLineLength;
						var rectStartY = endY + ArrowLineLength;
					}
					var arrowLine = svg.drawArrowLine(startX, startY, endX, endY);
					rectLeft = endX + rectW / 2;
					rectLeftArr.push([ rectLeft, ArrowLineLength ]);
					// 矩形框
					var rect = svg.drawRect(rectStartX, rectStartY, rectW, rectH).data("time", descstr).hover(function(e) {
						var mousePos = getMousePosition(e);
						var strd = this.data("time");
						var brNum = strd.split("br").length;
						$("#span").html(strd);
						var strdWidth = $("#span").width()+10;
						tipDiv.css({
							"height" : rectH * brNum,
							"width":strdWidth,
							"display" : "block",
							"line-height":rectH+"px",
							"left" : mousePos.x + 10,
							"top" : mousePos.y - 10
						}).html(this.data("time"));
					}, function() {
						$("#tip").hide();
					});
					//记录最右边位置
					if(maxRectPosX<rectStartX+rectW){
						maxRectPosX=rectStartX+rectW+150;
					}
					// 矩形框内的文本
					var txt = svg.drawText(rectStartX + 5, rectStartY + offsetY, str).attr({
						"font-size":fontSize
					}).data("time", descstr).hover(function(e) {
						var mousePos = getMousePosition(e);
						var strd = this.data("time");
						var brNum = strd.split("br").length;
						$("#span").html(strd);
						var strdWidth = $("#span").width()+10;
						tipDiv.css({
							"height" : rectH * brNum,
							"width":strdWidth,
							"display" : "block",
							"line-height":rectH+"px",
							"left" : mousePos.x + 10,
							"top" : mousePos.y - 10
						}).html(this.data("time"));
					}, function() {
						$("#tip").hide();
					});
					// 右上角关闭按钮
					var closeBtn = svg.drawText(rectStartX + rectW - 8, rectStartY + 10, "x").data("text",{"time":t,"name":str,"stationNO":i}).hover(function() {
						this.addClass("mouseon");
					}, function() {
						this.removeClass("mouseon");
					}).click(function() {
						this.parent().remove();
						removeDrawData(this.data("text"));//移除相应的数据，防止在放缩重新渲染时把移除的部分给渲染出来
					});
					g.add(redLine, arrowLine, rect, txt, closeBtn);
					svgGroup.add(g);
					// 判断上面矩形框是否超出画布范围，如果上面超出，就整体下移，或者下面超出就增加svg高度
					if (rectStartY < minRectPos) {
						minRectPos = rectStartY-50;
					}
					if (rectStartY + rectH > maxRectPos) {
						maxRectPos = rectStartY + rectH + 150;
					}
				}
			}
		}
		svgGroup.transform(svg.translate(0, -minRectPos));
		$("#svg").height(maxRectPos - minRectPos);
		$("#svg").width(maxRectPosX+500);
		$("#actSvg").css({
			"height" : maxRectPos - minRectPos,
			"overflow" : "hidden"
		});
		if (iframe.length == 1) {
			parent.reinitIframe();
		}
	}
	
	//点叉号同时删除数组中的数据
	function removeDrawData(selectedDataObj){
		var selectedStatData=drawData[selectedDataObj.stationNO][selectedDataObj.time];
		for(var i in selectedStatData){
			if(selectedDataObj.name==selectedStatData[i][0]){
				if(selectedStatData.length>1){
					//如果不止这一个元素
					selectedStatData.splice(i,1);
				}else{
					//如果只有这一个元素要把这一项全部删除
					delete drawData[selectedDataObj.stationNO][selectedDataObj.time];
				}
			}
		}
	}
	// 提示框
	var tipDiv = $("<div></div>").attr("id", "tip").css({
		"width" : 95,
		"height" : 30,
		"font-size":fontSize+2+"px",
		"background" : "rgba(0,0,0,.5)",
		"color" : "white",
		"text-align" : "center",
		"line-height":"30px",
		"position" : "absolute"
	}).appendTo("body").hide();
	// 获取鼠标位置
	function getMousePosition(e) {
		var ev = e || window.event;
		if (ev.pageX || ev.pageY) {
			return {
				x : ev.pageX,
				y : ev.pageY
			};
		}
		return {
			x : ev.clientX + doucument.body.scrollLeft - document.body.clientLeft,
			y : ev.clentY + document.body.scrollTop - document.body.clientTop
		};
	}
	
	
}